import {defineStore} from "pinia";
import {ref} from "vue";

export const useAuthStore = defineStore(
    "auth",
    () => {

        const user = ref(null);
        const token = ref(null);

        const isAuthenticated = computed(() => !!token.value);
        // 用户是否初始化
        const isUserInitialized = computed(() => !!user.value);
        // 获取用户信息
        function getUser() {
            return user.value;
        }

        function setUser(userInfo) {
            user.value = userInfo;
        }

        function setToken(authToken) {
            token.value = authToken;
        }

        function logout() {
            user.value = null;
            token.value = null;
        }

        return {user, token, isAuthenticated, isUserInitialized, setUser, setToken, logout};
    },
    {
        persist: true,
    }
);